﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AccountManagement.Entities;
using System.DirectoryServices;
using AccountManagement.Utility;

namespace AccountManagement.Managers
{
    public class UserManager
    {

        public static List<User> GetADUsers()
        {
            List<User> lstADUsers = new List<User>();
            try
            {                
                string DomainPath = String.Format("LDAP://{0}",Constants.DOMAIN_DN);
                DirectoryEntry searchRoot = new DirectoryEntry(DomainPath); 
                DirectorySearcher search = new DirectorySearcher(searchRoot);
                search.Filter = "(&(objectClass=user)(objectCategory=person))";
                search.PropertiesToLoad.Add(Constants.PROP_NAME);
                search.PropertiesToLoad.Add(Constants.PROP_GIVEN_NAME);
                search.PropertiesToLoad.Add(Constants.PROP_SN); //last name
                search.PropertiesToLoad.Add(Constants.PROP_SAM_ACCOUNT_NAME);
                search.PropertiesToLoad.Add(Constants.PROP_MAIL);
                search.PropertiesToLoad.Add(Constants.PROP_TELEPHONE_NUMBER);
                search.PropertiesToLoad.Add(Constants.PROP_DISPLAY_NAME);//first name
                search.PropertiesToLoad.Add(Constants.PROP_ACCOUNT_EXPIRE);
                search.PropertiesToLoad.Add(Constants.PROP_USER_ACCOUNT_CONTROL);

                SearchResult result;
                SearchResultCollection resultCol = search.FindAll();
                if (resultCol != null)
                {
                    for (int counter = 0; counter < resultCol.Count; counter++)
                    {
                        string UserNameEmailString = string.Empty;
                        result = resultCol[counter];
                        
                        User newUser = new User();
                        newUser.Name = Helper.GetStringValue(result, Constants.PROP_NAME);
                        newUser.FirstName = Helper.GetStringValue(result, Constants.PROP_GIVEN_NAME);
                        newUser.LastName = Helper.GetStringValue(result, Constants.PROP_SN);
                        newUser.Email = Helper.GetStringValue(result, Constants.PROP_MAIL);
                        newUser.SamAccountName = Helper.GetStringValue(result, Constants.PROP_SAM_ACCOUNT_NAME);
                        newUser.TelephoneNumber = Helper.GetStringValue(result, Constants.PROP_TELEPHONE_NUMBER);

                        long date = Helper.GetInt64Value(result, Constants.PROP_ACCOUNT_EXPIRE);
                        if (date > 0 && date < 9223372036854775807)
                        {
                            newUser.AccountExpires = DateTime.FromFileTime(date).ToString();
                        }
                        
                        newUser.Enabled = Helper.IsActive(result, Constants.PROP_USER_ACCOUNT_CONTROL);

                        lstADUsers.Add(newUser);
                    }
                }

                return lstADUsers;
            }
            catch (Exception ex)
            {
                
            }

            return lstADUsers;
        }
    }
}